import { Table, Tag, Space,Button} from 'antd';
import { connect } from 'dva';

function Tables({ customers, dispatch }) {
  const columns = [
    {
      title: '角色id',
      dataIndex: 'key',
      key: 'key',
    },
    {
      title: '姓名',
      dataIndex: 'firstName',
      key: 'firstName',
    },
    {
      title: '姓氏',
      dataIndex: 'lastName',
      key: 'lastName',
    },
    {
      title: '年龄',
      dataIndex: 'age',
      key: 'age',
    },
    {
      title: '地址',
      dataIndex: 'address',
      key: 'address',
    },
    {
      title: 'Tags',
      key: 'tags',
      dataIndex: 'tags',
      render: (tags) => (
        <>
          {tags.map((tag) => {
            let color = tag.length > 5 ? 'geekblue' : 'green';
            if (tag === 'loser') {
              color = 'volcano';
            }
            return (
              <Tag color={color} key={tag}>
                {tag.toUpperCase()}
              </Tag>
            );
          })}
        </>
      ),
    },
    {
      title: 'Action',
      key: 'action',
      render: (_, record) => (
        <Space size="middle">
          <a>Invite</a>
          <a
            style={{ color: 'red' }}
            onClick={() =>
              dispatch({
                type: 'customers/deleteTable',
                payload: {
                  key: record.key,
                },
              })
            }
          >
            Delete {record.key}
          </a>
        </Space>
      ),
    },
  ];
  function useInit() {
    dispatch({
      type: 'customers/getAll',
    });
  }

  function useCommit() {
    dispatch({
      type: 'customers/updateValue',
    });
  }
  return (
    <div>
      <div style={{ marginBottom: '10px' }}>
        <Button onClick={useInit}>初始化数据</Button>
        <Button onClick={useCommit} style={{ marginLeft: '10px' }}>
          Commit
        </Button>
      </div>
      <Table dataSource={customers} columns={columns}></Table>
    </div>
  );
}

export default connect(({ customers }) => ({ customers }))(Tables);
